home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / asm / fdtools11.lha / fdparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-27  |  9.1 KB  |  399 lines

  1. /* fdparse.c */
  2.  
  3. #include "fdparse.h"
  4.  
  5. #define EOF -1
  6.  
  7. BOOL TagCallName(struct fd *fd)
  8.  
  9. {
  10.   LONG len;
  11.  
  12.   if(!fd->fd_IsTagFunc) return(FALSE);
  13.  
  14.   if(strcmp(fd->fd_Function,"VFWritef") == 0 ||
  15.      strcmp(fd->fd_Function,"VFPrintf") == 0 ||
  16.      strcmp(fd->fd_Function,"VPrintf") == 0) {
  17.     STRPTR s;
  18.  
  19.     for(s = fd->fd_Function;*s;s++) *s = *(s+1);
  20.     return(TRUE);
  21.   }
  22.  
  23.   len = strlen(fd->fd_Function);
  24.  
  25.   if(len > 1 && fd->fd_Function[len-1] == 'A') {
  26.     fd->fd_Function[len-1] = '\0';
  27.   }
  28.   else {
  29.     if(len > 7 && strcmp(fd->fd_Function+len-7,"TagList") == 0) {
  30.       fd->fd_Function[len-4] = 's';
  31.       fd->fd_Function[len-3] = '\0';
  32.     }
  33.     else {
  34.       strcat(fd->fd_Function,"Tags");
  35.     }
  36.   }
  37.   return(TRUE);
  38. }
  39.  
  40. BOOL LibCallAlias(struct fd *fd)
  41.  
  42. {
  43.   LONG len;
  44.  
  45.   if(!fd->fd_IsTagFunc) return(FALSE);
  46.  
  47.   if(strcmp(fd->fd_Function,"VFWritef") == 0 ||
  48.      strcmp(fd->fd_Function,"VFPrintf") == 0 ||
  49.      strcmp(fd->fd_Function,"VPrintf") == 0) return(FALSE);
  50.  
  51.   len = strlen(fd->fd_Function);
  52.  
  53.   if(len > 1 && fd->fd_Function[len-1] == 'A') return(FALSE);
  54.  
  55.   if(len > 7 && strcmp(fd->fd_Function+len-7,"TagList") == 0) {
  56.     fd->fd_Function[len-7] = '\0';
  57.     if(strcmp(fd->fd_Function,"OpenWindow") == 0 ||
  58.        strcmp(fd->fd_Function,"OpenScreen") == 0) return(FALSE);
  59.   }
  60.   else {
  61.     strcat(fd->fd_Function,"TagList");
  62.   }
  63.   return(TRUE);
  64. }
  65.  
  66. void InitFD(BPTR fdfile,struct fd *fd)
  67.  
  68. {
  69.   fd->fd_Input = fdfile;
  70.   fd->fd_State = FD_PARSING|FD_PUBLIC;
  71.   fd->fd_Offset = 0;
  72.   fd->fd_NumParams = 0;
  73.   fd->fd_BaseName[0] = 0;
  74.   fd->fd_Function[0] = 0;
  75.   fd->fd_IsTagFunc = FALSE;
  76. }
  77.  
  78. #define BUFFLEN 1023
  79.  
  80. static UBYTE buff[BUFFLEN+1];
  81.  
  82. #define isstart(c) (((c)>='A'&&(c)<='Z')||((c)>='a'&&(c)<='z')||(c)=='_')
  83. #define isdigit(c) ((c)>='0'&&(c)<='9')
  84. #define iscont(c)  (isstart(c)||isdigit(c))
  85.  
  86. int keyword(struct fd *fd)
  87.  
  88. {
  89.   int i = 0;
  90.   int j = 0;
  91.  
  92.   while(buff[i] && buff[i] != ' ' && buff[i] != '\t' && i < IDLEN) {
  93.     fd->fd_Function[i] = buff[i];
  94.     i++;
  95.   }
  96.   fd->fd_Function[i] = 0;
  97.   
  98.   if(strcmp(fd->fd_Function,"base") == 0) {
  99.     if(fd->fd_BaseName[0]) {
  100.       strcpy(fd->fd_Function,"Library base name already defined !");
  101.       return(0);
  102.     }
  103.     while(buff[i] == ' ' || buff[i] == '\t') i++;
  104.     if(!isstart(buff[i])) {
  105.       strcpy(fd->fd_Function,"Illegal library base name !");
  106.       return(0);
  107.     }
  108.     do fd->fd_BaseName[j++] = buff[i++]; 
  109.     while(j < IDLEN && iscont(buff[i]));
  110.     fd->fd_BaseName[j] = 0; 
  111.  
  112.     if(j == IDLEN) {
  113.       strcpy(fd->fd_Function,"Library base name too long !");
  114.       return(0);
  115.     }
  116.  
  117.     while(buff[i] == ' ' || buff[i] == '\t') i++;
  118.  
  119.     if(buff[i]) {
  120.       strcpy(fd->fd_Function,"Syntax error in base name definition !");
  121.       return(0);
  122.     }
  123.     return(!0);
  124.   }
  125.  
  126.   if(strcmp(fd->fd_Function,"end") == 0) {
  127.  
  128.     if(!(fd->fd_BaseName[0])) {
  129.       strcpy(fd->fd_Function,"Unexpected end of fd-file !");
  130.       return(0);
  131.     }
  132.     fd->fd_State |= FD_READY;
  133.     return(!0);
  134.   }
  135.  
  136.   if(strcmp(fd->fd_Function,"bias") == 0) {
  137.     while(buff[i] == ' ' || buff[i] == '\t') i++;
  138.     fd->fd_Offset = 0;
  139.     while(isdigit(buff[i])) 
  140.       fd->fd_Offset = 10 * fd->fd_Offset - (buff[i++] - '0');
  141.     while(buff[i] == ' ' || buff[i] == '\t') i++;
  142.     if(buff[i] || fd->fd_Offset == 0) {
  143.       strcpy(fd->fd_Function,"Illegal bias value !");
  144.       return(0);
  145.     }
  146.     fd->fd_State |= FD_BIAS;
  147.     return(!0);
  148.    }
  149.  
  150.   if(strcmp(fd->fd_Function,"public") == 0) {
  151.     while(buff[i] == ' ' || buff[i] == '\t') i++;
  152.     if(buff[i]) {
  153.       strcpy(fd->fd_Function,"Syntax error in ##public keyword !");
  154.       return(0);
  155.     }
  156.     fd->fd_State &= ~FD_PRIVATE;
  157.     return(!0);
  158.   }
  159.  
  160.   if(strcmp(fd->fd_Function,"private") == 0) {
  161.     while(buff[i] == ' ' || buff[i] == '\t') i++;
  162.     if(buff[i]) {
  163.       strcpy(fd->fd_Function,"Syntax error in ##private keyword !");
  164.       return(0);
  165.     }
  166.     fd->fd_State |= FD_PRIVATE;
  167.     return(!0);
  168.   }
  169.  
  170.   strcpy(fd->fd_Function,"Unknown keyword !");
  171.   return(0);
  172. }
  173.  
  174. #define isdreg(c) ((c)>='0'&&(c)<='7')
  175. #define isareg(c) ((c)>='0'&&(c)<='5')
  176.  
  177. static BOOL IsTagFunc(struct fd *fd,STRPTR lastparm,LONG len)
  178.  
  179. {
  180.   LONG l;
  181.  
  182.   if(strnicmp(lastparm,"tags",len) == 0 ||
  183.      strnicmp(lastparm,"taglist",len) == 0) return(TRUE);
  184.  
  185.   if(strcmp(fd->fd_Function,"CachePreDMA") == 0 ||
  186.      strcmp(fd->fd_Function,"CachePostDMA") == 0) return(FALSE);
  187.  
  188.   l = strlen(fd->fd_Function);
  189.  
  190.   if((l > 1 && fd->fd_Function[l-1] == 'A') ||
  191.      (l > 7 && stricmp(fd->fd_Function+l-7,"TagList")  == 0)) return(TRUE);
  192.  
  193.   if(strcmp(fd->fd_Function,"VFWritef") == 0 ||
  194.      strcmp(fd->fd_Function,"VFPrintf") == 0 ||
  195.      strcmp(fd->fd_Function,"VPrintf") == 0) return(TRUE);
  196.  
  197.   return(FALSE);
  198. }
  199.  
  200. int function(struct fd *fd)
  201.  
  202. {
  203.   int i = 0;
  204.   int j = 0;
  205.   UWORD regs = 0;
  206.   UBYTE numargs;
  207.  
  208.   if(!isstart(buff[0])) {
  209.     strcpy(fd->fd_Function,"Illegal function name !");
  210.     return(0);
  211.   }
  212.  
  213.   do fd->fd_Function[j++] = buff[i++]; 
  214.   while(j < IDLEN && iscont(buff[i]));
  215.   fd->fd_Function[j] = 0; 
  216.  
  217.   if(j == IDLEN) {
  218.     strcpy(fd->fd_Function,"Function name too long !");
  219.     return(0);
  220.   }
  221.  
  222.   if(buff[i++] != '(') {
  223.     strcpy(fd->fd_Function,"Syntax error in function definition !");
  224.     return(0);
  225.   }
  226.  
  227.   j = i;
  228.  
  229.   numargs = buff[i] != ')' ? 1 : 0;
  230.  
  231.   while(buff[i] && buff[i] != ')') {
  232.     if(buff[i] == ',') {
  233.       j = ++i;
  234.       numargs++;
  235.     }
  236.     else i++;
  237.   }
  238.   
  239.   if(numargs)
  240.     fd->fd_IsTagFunc = IsTagFunc(fd,buff+j,i-j);
  241.   else
  242.     fd->fd_IsTagFunc = FALSE;
  243.  
  244.   i++;
  245.  
  246.   if(buff[i++] != '(') {
  247.     strcpy(fd->fd_Function,"Syntax error in function definition !");
  248.     return(0);
  249.   }
  250.  
  251.   if(buff[i] == ')') {
  252.     fd->fd_NumParams = 0;
  253.     i++;
  254.   }
  255.   else {
  256.     for(j = 0;j < 14;j++) {
  257.       switch(buff[i++]) {
  258.         case 'D':
  259.         case 'd':
  260.           if(!isdreg(buff[i])) {
  261.             strcpy(fd->fd_Function,"Syntax error in function definition !");
  262.             return(0);
  263.           }
  264.           fd->fd_Parameter[j] = REG_D(buff[i]-'0');
  265.           break;
  266.         case 'A':
  267.         case 'a':
  268.           if(!isareg(buff[i])) {
  269.             strcpy(fd->fd_Function,"Syntax error in function definition !");
  270.             return(0);
  271.           }
  272.           fd->fd_Parameter[j] = REG_A(buff[i]-'0');
  273.           break;  
  274.         default:
  275.           strcpy(fd->fd_Function,"Syntax error in function definition !");
  276.           return(0);
  277.       } 
  278.   
  279.       if(regs & (1 << fd->fd_Parameter[j])) {
  280.         strcpy(fd->fd_Function,"Register used twice in function definition !");
  281.         return(0);
  282.       } 
  283.  
  284.       regs |= (1 << fd->fd_Parameter[j]);
  285.  
  286.       i++;
  287.  
  288.       switch(buff[i++]) {
  289.         case ',':
  290.         case '/':
  291.           break;
  292.         case ')':
  293.           goto ct;
  294.         default:
  295.           strcpy(fd->fd_Function,"Syntax error in function definition !");
  296.           return(0);
  297.       }
  298.     }
  299.  
  300. ct:
  301.     if(j == 14) {
  302.       strcpy(fd->fd_Function,"Function has too many arguments !");
  303.       return(0);
  304.     }
  305.  
  306.     fd->fd_NumParams = j + 1;
  307.     if(fd->fd_NumParams != numargs) {
  308.       strcpy(fd->fd_Function,"Wrong number of registers specified !");
  309.       return(0);
  310.     }
  311.   }
  312.  
  313.   while(buff[i] == ' ' || buff[i] == '\t') i++;
  314.  
  315.   if(buff[i]) {
  316.     strcpy(fd->fd_Function,"Syntax error in function definition !");
  317.     return(0);
  318.   }
  319.  
  320.   if(fd->fd_State & FD_BIAS) fd->fd_State &= ~FD_BIAS;
  321.   else fd->fd_Offset -= 6;
  322.  
  323.   return(!0);
  324. }
  325.  
  326. int ParseFD(struct fd *fd)
  327.  
  328. {
  329.   int c,i;
  330.  
  331.   for(;;) {
  332.     c = FGetC(fd->fd_Input);
  333.  
  334.     switch(c) {
  335.       case '*':
  336.         i = 0;
  337.         do {
  338.           fd->fd_Function[i++] = c;
  339.           c = FGetC(fd->fd_Input);
  340.         } while(i < IDLEN-1 && c != '\n' && c != EOF);
  341.         if(c == EOF) {
  342.           strcpy(fd->fd_Function,"Unexpected end of fd-file !");
  343.           return(FD_ERROR);
  344.         }
  345.         if(c != '\n') {
  346.           strcpy(fd->fd_Function,"Comment too long !");
  347.           return(FD_ERROR);
  348.         }
  349.         fd->fd_Function[i] = '\0';
  350.         return(FD_COMMENT);
  351.       case ' ':
  352.       case '\t':
  353.         while((c = FGetC(fd->fd_Input)) == ' ' || c == '\t');
  354.         if(c != '\n') {
  355.           strcpy(fd->fd_Function,"Syntax error in fd-file !");
  356.           return(FD_ERROR);
  357.         }
  358.       case '\n':
  359.         break;
  360.       case EOF:
  361.         strcpy(fd->fd_Function,"Unexpected end of fd-file !");
  362.         return(FD_ERROR);
  363.       case '#':
  364.         c = FGetC(fd->fd_Input);
  365.         if(c != '#') {
  366.           strcpy(fd->fd_Function,"Syntax error in keyword !");
  367.           return(FD_ERROR);
  368.         }
  369.         i = 0;
  370.         while((c = FGetC(fd->fd_Input)) != EOF && c != '\n' && i < BUFFLEN) 
  371.           buff[i++] = c;
  372.         buff[i] = 0;
  373.         if(!keyword(fd)) return(FD_ERROR);
  374.         if(c == EOF && 
  375.            (!(fd->fd_State&FD_READY) || !(fd->fd_BaseName[0]))) {
  376.           strcpy(fd->fd_Function,"Unexpected end of fd-file !");
  377.           return(FD_ERROR);
  378.         }
  379.         return(FD_KEYWORD);
  380.       default:
  381.         if(c < 32) {     
  382.           strcpy(fd->fd_Function,"Illegal character in fd-file !");
  383.           return(FD_ERROR);
  384.         }
  385.         buff[0] = c;
  386.         i = 1;
  387.         while((c = FGetC(fd->fd_Input)) != EOF && c != '\n' && i < BUFFLEN)
  388.           buff[i++] = c;
  389.         buff[i] = 0;
  390.         if(c == EOF) {
  391.           strcpy(fd->fd_Function,"Unexpected end of fd-file !");
  392.           return(FD_ERROR);
  393.         }
  394.         if(!function(fd)) return(FD_ERROR);
  395.         return(FD_FUNCTION);
  396.     }
  397.   }
  398. }
  399.